<?php
if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
include_once( "class_6h/class.lotto6h.php" );
$lotto6h = new lotto6h();
$ui = new UI();
$type = intval($_GET['type']);
if ($type == 0)
{
    diemsg("不要随便攻击本站！你的 IP 已经被记录了！");
}
switch ($type)
{
    case 1 :
    case 2 :
        $bettype = 15;
        break;
    case 3 :
    case 4 :
        $bettype = 16;
        break;
    case 5 :
    case 6 :
    case 7 :
        $bettype = 17;
        break;
    case 8 :
    case 9 :
        $bettype = 23;
        break;
}
$tpl = new Template2();
$tpl->load(array("main" => "proc2.htm"));
$tpl->set("account", $curruser['account']);
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$rs = getdb("select * from lotto where open>='" . date("Y-m-d") . "' order by open");
if ($rs->eof)
{
    $ui->showErrorPage("<p class=error>目前没有可以下注期数！</p>");
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$lotto_ns = $rs->f['n1'] + $rs->f['n2'] + $rs->f['n3'] + $rs->f['n4'] + $rs->f['n5'] + $rs->f['n6'];
$conf = new Config();
$prot_start = $conf->get("protect_start");
$stopbet = $conf->get("waitbet");
if (date("Y-m-d") < $open)
{
    exit();
}
else if (date("Y-m-d") == $open)
{
    if ($stopbet <= date("Hi") || 0 < $lotto_ns)
    {
        $ui->showErrorPage("<p>下注期数：" . d3($round) . "<br>开奖日：" . $open . "</p>" . "<p class=error>已经截止下注！</p>");
        exit();
    }
    else if (date("Hi") <= $conf->get("startbet"))
    {
        exit();
    }
}
$tpl->set("open", $open);
$tpl->set("round", d3($round));
$tpl->set("lotto_id", $lotto_id);
$tpl->set("account", $curruser['account']);
$tpl->set("amount", $curruser['amount']);
$rs1 = getdb("select amount_6h from k_user_bet where user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id);
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set("usedamount", $usedamount);
$num = intval($_GET['num']);
if ($num == 0)
{
    write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击");
    err("不要随便攻击本站！你的 IP 已经被记录了！");
}
$tpl->set("num", $num);
$bet1 = 0;
$bet2 = 0;
$bet3 = 0;
$where1 = "";
switch ($type)
{
    case 1 :
    case 2 :
        $where1 = "(numbers='{$num} 1' or numbers='{$num} 2')";
        break;
    case 3 :
    case 4 :
        $where1 = "(numbers='{$num} 3' or numbers='{$num} 4')";
        break;
    case 5 :
    case 6 :
    case 7 :
        $where1 = "(numbers='{$num} 5' or numbers='{$num} 6' or numbers='{$num} 7')";
        break;
    case 8 :
    case 9 :
        $where1 = "(numbers='{$num} 8' or numbers='{$num} 9')";
        
        break;
}
$rs1 = getdb("select amount,numbers from bet where " . $where1 . " and bettype=" . $bettype . " and user_id=" . $curruser['id'] . " and lotto_id=" . $lotto_id . " and rame=0");
$betamount = 0;
while (!$rs1->eof)
{
    $tmp = explode(" ", $rs1->f['numbers']);
	
    switch ($tmp[1])
    {
        case 1 :
        case 3 :
        case 8 :
            $betamount += $rs1->f['amount'];
            $bet1 += $rs1->f['amount'];
            break;
        case 2 :
        case 4 :
        case 9 :
            $betamount += $rs1->f['amount'];
            $bet2 += $rs1->f['amount'];
            break;
        case 5 :
        case 6 :
        case 7 :
            $betamount += $rs1->f['amount'];
            $bet3 += $rs1->f['amount'];
            break;
        default :
            //echo "错误的注单！";
            break;
    }
    $rs1->movenext();
}
$tpl->set("betamount", $betamount);
$idisk = getUserValidDisk($curruser);
$disk = $g_disk_code[$idisk];
$tpl->set("diskStr", strtoupper($disk) . "盘");
$rs = getdb("select r15_" . $num . "_" . $type . " from rate15" . $disk . " where lotto_id={$lotto_id}");
$sub_rates = get_subs_rates($curruser['id'], $lotto_id, $bettype, $num . "_" . $type);
if ($rs->eof)
{
    err("系统目前维护中，请稍後！");
}
$rate = $rs->f[0] - $sub_rates - 0;
$tpl->set("rate", trimfloat($rate, 1));
$betmin = $conf->get("betmin");
$tpl->set("min", $betmin);
$betmax = $conf->get("betmax");
$tpl->set("max", $betmax);
$tpl->set("max_str", $betmax);
$bignum = array("", "一", "二", "三", "四", "五", "六");
$betname = "正码" . $bignum[$num];
switch ($type)
{
    case 1 :
        $betname .= "单";
        break;
    case 2 :
        $betname .= "双";
        break;
    case 3 :
        $betname .= "大";
        break;
    case 4 :
        $betname .= "小";
        break;
    case 5 :
        $betname .= "红";
        break;
    case 6 :
        $betname .= "蓝";
        break;
    case 7 :
        $betname .= "绿";
        break;
    case 8 :
        $betname .= "合单";
        break;
    case 9 :
        $betname .= "合双";
}
$tpl->set("betname", $betname);
$rs1 = getdb("select * from amounts where bettype={$bettype} and user_id=" . $curruser['id']);
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
$betallow = $amount2;
if ($type == 1 || $type == 3 || $type == 8)
{
    $betallow = $amount2 - $bet1;
}
else if ($type == 2 || $type == 4 || $type == 9)
{
    $betallow = $amount2 - $bet2;
}
else
{
    $betallow = $amount2 - $bet3;
}

$tpl->set("amount1", $amount1);
$tpl->set("amount2", $amount2);
$tpl->set("amount1_str", $amount1);
$tpl->set("amount2_str", $betallow);
$usemaxamount = $curruser['usemaxamount'];
$oneamount = Min($amount1, $betallow);
$tpl->set("oneamount", "<tr><td>单注限额：</td> <td align=right><b>" . $oneamount . "</b></td>");
if (ispost())
{
    $money = intval($_POST['money']);
    if ($money == 0)
    {
        write_exceptional_log($curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击");
        err("不要随便攻击本站！你的 IP 已经被记录了！");
    }
    if ($money < $betmin)
    {
        err("下注金额不能小於最低限额！");
    }
    if ($amount2 < $money + $betamount)
    {
        err("下注金额不能超过单项限额！");
    }
    if ($curruser['balance'] < $money)
    {
        err("下注金额不能超过信用额度！");
    }
    if ($usemaxamount && $curruser['maxamount'] < $money + $usedamount)
    {
        err("下注金额不能超过本期限额！");
    }
    if ($oneamount < $money)
    {
        err("下注金额不能超过单注限额！");
    }
    if ($conf->get("waitbet") <= date("Hi"))
    {
        err("已经截止下注！");
    }
    if (5 <= $type && $type <= 7)
    {
        if (3.6 < $rate)
        {
            err("不正常的赔率数值！");
        }
    }
    else if (2.9 < $rate)
    {
        err("不正常的赔率数值！");
    }
    $payoff = round($money * ( $rate - 1 ));
    $stime = systime();
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'{$num} {$type}'";
    $fields['amount'] = $money;
    $fields['rate'] = $rate;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $idisk;
    $fields['line_nums'] = "'{$num} {$type}'";
    $fields['unitmoney'] = $money;
    $u2 = get_user($curruser['prev_id']);
    $u3 = get_user($u2['prev_id']);
    $u4 = get_user($u3['prev_id']);
    $u5 = get_user($u4['prev_id']);
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack($curruser['id'], $fields['bettype'], $idisk, 1);
    $a2 = GetBack($u2['id'], $fields['bettype'], $idisk, 2);
    $a3 = GetBack($u3['id'], $fields['bettype'], $idisk, 3);
    $a4 = GetBack($u4['id'], $fields['bettype'], $idisk, 4);
    $a5 = GetBack($u5['id'], $fields['bettype'], $idisk, 5);
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $a6 = GetBack($u5['supe_account'], $fields['bettype'], $idisk, 255);
    $fields['back6'] = $a6;
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min($u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2']);
    $fields['percent4'] = min($u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3']);
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ($fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0)
    {
        err("占成参数设置错误，注单无法存档！");
        return;
    }
    $fields['r2'] = $money * $fields['percent2'] / 100;
    $fields['r3'] = $money * $fields['percent3'] / 100;
    $fields['r4'] = $money * $fields['percent4'] / 100;
    $fields['r6'] = $money * $fields['upercent'] / 100;
    $fields['ip'] = "'" . getIP() . "'";
    $newid = db_insert("bet", $fields);
    if (!$newid)
    {
        err("超过信用额度");
    }
	$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	if(!$rs->eof)
		getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	else
		getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
    check_autorate($lotto_id, $bettype, "{$num} {$type}", $money * ( $fields['upercent'] / 100 ));
    $lotto6h->getBet_6h($newid);
    $tpl->load("succ", "succ.htm");
    $tpl->set("ordertype", "<font color=red>" . $betname . "</font> @ <font color=red><b>" . trimfloat($rate, 1) . "</b></font>");
    $tpl->set("money", $money);
    $tpl->set("payoff", $payoff);
    $tpl->show("succ");
}
else
{
    if (ispost())
    {
        $money = intval($_POST['money']);
        $tpl->set("money", $money);
        $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
    }
    else
    {
        $oldrate = $_GET['rate'] - 0;
        if ($oldrate != $rate)
        {
            $tpl->set("message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>");
        }
    }
    $tpl->show("main");
}
?>